package com.yanxu;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/11/12-2024
 * @Description: 子线程加入， main主线程在等待的过程中被中断。
 */
public class JoinInterrupt {
    public static void main(String[] args)  {
        final Thread mainThread = Thread.currentThread();

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                try {
                    mainThread.interrupt(); //（此时主线程还在休眠等待的过程中）将主线程中断
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程运行完毕");
            }
        }, "子线程");

        thread1.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            System.out.println("主线程被中断了!!!");
        }
        System.out.println("主线程运行完毕");

    }

}